package com.woniuxy.course.controller;

import com.woniuxy.commons.entity.CourseChapter;
import com.woniuxy.commons.entity.Courseware;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.exception.EtmsServiceException;
import com.woniuxy.commons.exception.course.CoursewareNotFoundException;
import com.woniuxy.commons.utils.CodeUtils;
import com.woniuxy.course.service.CourseChapterService;
import com.woniuxy.course.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author LuoChen
 * @DateTime 2020-08-01 10:43:29
 * @Description 章节controller
 */
@RestController
public class CourseChapterController {

    @Autowired
    private CourseChapterService courseChapterService;

    /**
     * 根据章节id查课件
     * @param cpid
     * @return: com.woniuxy.commons.entity.ResponseEntity<java.util.List<com.woniuxy.commons.entity.Courseware>>
     * @author: LuoChen
     * @dateTime: 2020-08-03 17:53:40
     */
    @GetMapping("/findCoursewares/{cpid}")
    public ResponseEntity<List<Courseware>> findCoursewaresByCpid(@PathVariable("cpid") int cpid){
        ResponseEntity<List<Courseware>> listResponseEntity = new ResponseEntity<>();
        try {
            List<Courseware> coursewareByCpid = courseChapterService.findCoursewareByCpid(cpid);
            return listResponseEntity.setMessage("根据章节id查课件成功")
                    .setCode(CodeUtils.SUCCESS_CODE)
                    .setData(coursewareByCpid);
        }catch (EtmsServiceException e){
            System.out.println(e.toString());
            if (e instanceof CoursewareNotFoundException){
                return listResponseEntity.setCode(StatusCode.GET_NOTHING).setMessage("什么都没有");
            }else {
                return listResponseEntity.setMessage("系统错误").setCode(StatusCode.SERVER_ERROR);
            }
        }

    }

    /**
     * 根据课程id查找章节list
     * @param cid
     * @return: com.woniuxy.commons.entity.ResponseEntity<java.util.List<com.woniuxy.commons.entity.CourseChapter>>
     * @author: LuoChen
     * @dateTime: 2020-08-03 17:56:59
     */
    @GetMapping("/findChapterByCid/{cid}")
    public ResponseEntity<List<CourseChapter>> findChapterByCid(@PathVariable("cid") int cid){
        ResponseEntity<List<CourseChapter>> listResponseEntity = new ResponseEntity<>();
        try {
            List<CourseChapter> chapterByCpid = courseChapterService.findChaptersByCid(cid);
            return listResponseEntity.setMessage("根据id查章节成功")
                    .setCode(CodeUtils.SUCCESS_CODE)
                    .setData(chapterByCpid);
        }catch (EtmsServiceException e){
            System.out.println(e.toString());
            if (e instanceof CoursewareNotFoundException){
                return listResponseEntity.setCode(StatusCode.GET_NOTHING).setMessage("什么都没有");
            }else {
                return listResponseEntity.setMessage("系统错误").setCode(StatusCode.SERVER_ERROR);
            }
        }
    }
}
